package com.twitterbuzz.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.twitterbuzz.shared.Result;
import com.twitterbuzz.shared.TweetInfo;
import com.twitterbuzz.shared.UserInfoClient;

public class HomeScreen extends Composite{
	private VerticalPanel mainPanel;
	private HTML signOutLink = new HTML("<a href='javascript:;'>Sign Out</a>");
	private HTML aboutLink = new HTML("<a href='javascript:;'>About</a>");	
	private Label locationLabel, messageLabel, descriptionLabel, welcomeLabel;
	public String rootUserName;
	public HomeScreen(UserInfoClient user)    {		
		rootUserName = user.name;
		mainPanel = new VerticalPanel();
	    mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    HorizontalPanel links = new HorizontalPanel();
	    links.setSpacing(4);
	    links.add(signOutLink);
	    links.add(aboutLink);	    	   
	    
	    signOutLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				//Create the Login screen
			      LoginScreen scrLogin=new LoginScreen();
			      RootPanel.get().clear();
			      //Attach it to the root panel
			      RootPanel.get().add(scrLogin);			      
			}
		});
	    
		welcomeLabel = new Label();
		messageLabel = new Label();		
		descriptionLabel = new Label();		
		locationLabel = new Label();		
		welcomeLabel.setText(user.name);
		welcomeLabel.setStyleName("label");		
		descriptionLabel.setText(user.description);
		descriptionLabel.setStyleName("label");
		locationLabel.setText(user.location);
		locationLabel.setStyleName("label");
		Image widget = new Image(user.profileUrl);
		widget.setStyleName("paddingtop");
		// widget.setSize("50px", "100px");
		// mainPanel.setStyleName("padding");
		mainPanel.add(links);
		mainPanel.add(widget);
		mainPanel.add(welcomeLabel);		
		mainPanel.add(messageLabel);
		mainPanel.add(descriptionLabel);
		mainPanel.add(locationLabel);
		initWidget(mainPanel);
		getResults(user.name);
	}
			
public static native void pass(int i,String rootUserName, String username,String sentiment,String topic,String tweet)/*-{
   	if(i==0)
   	{
   		$wnd.vari = new Array();
   		$wnd.rootUserName = rootUserName;
   		
   		$wnd.topiclist = new Array();
   		$wnd.vari[0] = new Object();
   		$wnd.vari[0].name = username;
   		$wnd.vari[0].sentiment = sentiment;
   		$wnd.vari[0].topic = topic;
   		$wnd.n=i;
   		$wnd.topiclist[0] = topic;
   	}
   	else
   	{
   		$wnd.vari[i] = new Object();
   		$wnd.vari[i].name = username;
   		$wnd.vari[i].sentiment = sentiment;
   		$wnd.vari[i].topic = topic;
   		$wnd.n=i;
   		j = 0;
		newtopic = true;
   		while($wnd.topiclist[j] != null)
   		{
   			if(topic == $wnd.topiclist[j])
   				newtopic = false;
   			j++;
   		}
   		if(newtopic != false)
   			$wnd.topiclist[j] = topic;
   	}
}-*/;
	public void getResults(String username) {
		TwitterBuzzServiceAsync nbService = (TwitterBuzzServiceAsync) GWT
				.create(TwitterBuzzService.class);
		AsyncCallback<Result[]> callback = new AsyncCallback<Result[]>() {
			@Override
			public void onSuccess(Result[] results) {
				
				Tree t = new Tree();
				int i = 0;
				
				for (Result result : results) {
					
					for (TweetInfo ti : result.tweetInfos) {
						
						if (ti == null) continue;
						pass(i,rootUserName,ti.user.name,ti.sentiment.toString(),result.trend,ti.tweet);
						i=i+1;
					/*	HorizontalPanel tweetPanel = new HorizontalPanel();
						tweetPanel.setStyleName("paddingleft");
						tweetPanel.setSpacing(5);
						Label user = new Label(ti.user.name);
						user.setStyleName("error");
						Label tweet = new Label(ti.tweet);
						tweet.setStyleName("label");
						Label sentiment = new Label(ti.sentiment.toString());
						sentiment.setStyleName("error");
						tweetPanel.add(user);
						tweetPanel.add(tweet);
						tweetPanel.add(sentiment);
						root.addItem(tweetPanel);*/
					}
					
					
				}
				mainPanel.clear();
				TreeItem root = new TreeItem();
				root.setHTML("<button type='button' class='removeMe' onClick='test(rootUserName,vari,topiclist,n)'>GO</button>");
				t.addItem(root);
				TreeItem canvas = new TreeItem();
				canvas.setHTML("<table border='0'><tr><td><canvas id='viewport' width='600' height='600'></canvas></td><td><div id='here' style='display: none;'>Tweets:<br><div id='tweetshere'></div></div></td></table>");
				
				t.addItem(canvas);
				mainPanel.add(t);
				//t.removeItem(root);
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		};
		nbService.getResults(username, callback);		
	}
}
